﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Common;
using StoragePeriod.Web.UserControls;
using StoragePeriod.Model.RawDict;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.BLL.FoodTesting;
using System.Data;
using StoragePeriod.Web.NopiExcel;
using StoragePeriod.Web.Nutritionknowledge;
using System.Text;
using StoragePeriod.Model.StorageForecast;
using StoragePeriod.BLL.StorageForecast;

namespace StoragePeriod.Web.StorageForecast.DataSearch
{
    public partial class DataSearchList : SecurityPage
    {
        T_QualityResult tQualityResult = new T_QualityResult();//计算结果操作
        T_QualityCalculateLog tQualityCalculateLog = new T_QualityCalculateLog();//计算日志操作


        protected void Page_Load(object sender, EventArgs e)
        {
            LeftTree.SelectedChanging += new MaterialTree.TreeEventDelegate(LeftTree_SelectedChanging);
            LeftLable.Text = "贮存期数据查询";
            if (!IsPostBack)
            {
                this.LoadDrop();
                //默认班组
                SYS_Users md = SYS_Users.Where("UserCode=@0", User.Identity.Name).First();
                OperationTeam team = OperationTeam.Where("id=@0", Convert.ToInt32(md.UserType)).First();
                this.Drop_OperationSearch.SelectedValue = team.ParentId.ToString();
                this.Drop_OperationSearch_SelectedIndexChanged(null, null);
                this.Drop_TeamSearch.SelectedValue = team.id.ToString();
            }
        }

        //加载对应的班组
        private void LoadDrop()
        {
            List<OperationTeam> OperationList = OperationTeam.Where("ParentId=@0", 0).OrderByDescending(p => p.id).ToList();
            this.Drop_OperationSearch.DataSource = OperationList;
            this.Drop_OperationSearch.DataValueField = "id";
            this.Drop_OperationSearch.DataTextField = "OTName";
            this.Drop_OperationSearch.DataBind();
            this.Drop_OperationSearch_SelectedIndexChanged(null, null);
        }

        //加载班组
        protected void Drop_OperationSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.Drop_OperationSearch.SelectedItem != null)
            {
                int ParentId = Convert.ToInt32(this.Drop_OperationSearch.SelectedValue);
                List<OperationTeam> TeamList;
                if (ParentId == -1)
                {
                    TeamList = new List<OperationTeam>();
                }
                else
                {
                    TeamList = OperationTeam.Where("ParentId=@0", ParentId).ToList();
                }
                this.Drop_TeamSearch.DataSource = TeamList;
                this.Drop_TeamSearch.DataValueField = "id";
                this.Drop_TeamSearch.DataTextField = "OTName";
                this.Drop_TeamSearch.DataBind();
            }
        }


        protected void LeftTree_SelectedChanging(Dictionary<string, string> dic)
        {
            StringBuilder RawIDList = new StringBuilder(); //选择的原料ID集合
            this.HidRawList.Value = "0";
            if (dic.Count > 0)
            {
                int Index = 0;
                DataTable SelectRawList = InitDt();//获取选择原料初始Table
                dic.ToList().ForEach(p =>
                {
                    Index += 1;
                    DataRow row = SelectRawList.NewRow();
                    row["id"] = Index;
                    row["RID"] = Index;
                    row["RawName"] = p.Value;
                    row["AddTime"] = "";
                    row["CalculateTypeID"] = "";
                    row["QuotaName"] = "";
                    row["ElectValue"] = "";
                    row["CalculateResult"] = "";
                    SelectRawList.Rows.Add(row);
                    RawIDList.Append(p.Key.ToString() + ",");
                });
                this.RepList.DataSource = SelectRawList;
                this.RepList.DataBind();
            }
            if (RawIDList.Length > 0)
            {
                this.HidRawList.Value = RawIDList.ToString().TrimEnd(',');
            }
        }

        //获取估算类型 估算类型ID（1：品质估算、2：贮存期预测、3：贮存条件选择）
        public string GetCalculateName(string CalculateTypeID)
        {
            switch (CalculateTypeID)
            { 
                case "1":
                    return "品质估算";
                case "2":
                    return "贮存期预测";
                case "3":
                    return "贮存条件选择";
                default:
                    return "";
            }
        }

        //获取初始化Table
        private DataTable InitDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("RID");
            dt.Columns.Add("RawName");
            dt.Columns.Add("AddTime");
            dt.Columns.Add("CalculateTypeID");
            dt.Columns.Add("QuotaName");
            dt.Columns.Add("ElectValue");
            dt.Columns.Add("CalculateResult");
            return dt;
        }

        //数据绑定
        private void RepDataBing()
        { 
            int SearchTypeID = 0;//默认为0 ：贮存期数据查询 1：贮存期计算日志
            if (Request.QueryString["SearchTypeID"] != null)
            {
                SearchTypeID = int.Parse(Request.QueryString["SearchTypeID"].ToString());
            }
            StringBuilder sqlWhere = new StringBuilder();
            sqlWhere.Append("RawID in ("+this.HidRawList.Value+") ");
            if (this.txtStartTime.Text.Trim() != "")
            {
                sqlWhere.Append(" and AddTime>=CAST('" + this.txtStartTime.Text.Trim() + "' as datetime) ");
            }
            if (this.txtEndTime.Text.Trim() != "")
            {
                DateTime EndTime = Convert.ToDateTime(this.txtEndTime.Text.Trim()).AddDays(1);
                sqlWhere.Append(" and AddTime<=CAST('" + EndTime.ToShortDateString() + "' as datetime)");
            }

            if (this.Drop_TeamSearch.SelectedItem != null) //作业区
            {
                sqlWhere.Append(" and TeamID="+this.Drop_TeamSearch.SelectedValue);
            }
            else if (this.Drop_OperationSearch.SelectedItem != null) //班组
            {
                sqlWhere.Append(" and OAreaID=" + this.Drop_OperationSearch.SelectedValue);
            }


            if (SearchTypeID == 0)
            {
                this.AspNetPager1.RecordCount = tQualityResult.GetPageCount(sqlWhere.ToString());
                E_QualityResult eQualityResult=new E_QualityResult();
                eQualityResult.PageIndex=this.AspNetPager1.CurrentPageIndex;
                eQualityResult.PageSize=this.AspNetPager1.PageSize;
                eQualityResult.SearchWhere=sqlWhere.ToString();
                //List<E_QualityResult> list = tQualityResult.GetModelList(sqlWhere.ToString());
                DataTable dt = tQualityResult.GetPageList(eQualityResult);
                this.RepList.DataSource = dt;
                this.RepList.DataBind();
            }
            else
            {
                this.AspNetPager1.RecordCount = tQualityCalculateLog.GetPageCount(sqlWhere.ToString());
                E_QualityCalculateLog eQualityResult = new E_QualityCalculateLog();
                eQualityResult.PageIndex = this.AspNetPager1.CurrentPageIndex;
                eQualityResult.PageSize = this.AspNetPager1.PageSize;
                eQualityResult.SearchWhere = sqlWhere.ToString();
                //List<E_QualityCalculateLog> list = tQualityCalculateLog.GetModelList(sqlWhere.ToString());
                DataTable dt = tQualityCalculateLog.GetPageList(eQualityResult);
                this.RepList.DataSource = dt;
                this.RepList.DataBind();
            }
        }

        //查询符合要求的数据
        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.RepDataBing();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepDataBing();
        }
    }
}